package service

import (
	"chat/global"
	"chat/internal/model"
)

func GetUsersByGroupId(group int) ([]int, error) {
	var list []int
	err := global.DB.Raw(`select user_id from group_members where group_id = ?`, group).Scan(&list).Error
	return list, err
}

func GetAllGroupMember(uid int) ([]model.GroupMember, error) {
	var list []model.GroupMember
	err := global.DB.Raw(`select t1.*
from group_members
inner join group_members as t1 on t1.group_id = group_members.group_id
where group_members.user_id = ?`, uid).Scan(&list).Error
	return list, err
}

func GetGroupsByUserId(uid int) ([]int, error) {
	var list []int
	err := global.DB.Raw(`select group_id from group_members where user_id = ?`, uid).Scan(&list).Error
	return list, err
}

func AddMember(uid, groupId int) error {
	var o = model.GroupMember{
		UserId:  uid,
		GroupId: groupId,
	}
	return global.DB.Create(&o).Error
}
